package houtai.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import houtai.mapper.VideoInfoMapper;
import houtai.pojo.UserInfo;
import houtai.pojo.Video;
import houtai.pojo.VideoInfo;
import houtai.util.ListPageUtil;
import houtai.vo.PageResult;
import houtai.vo.VideoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class VideoInfoService {
    @Autowired
    private VideoInfoMapper videoInfoMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private MongoTemplate mongoTemplate;

    public PageResult queryVideoInfoList(Integer page, Integer pagesize, String sortProp, String sortOrder, Long userId) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);

        List<Video> videoList = queryVideoByUserId(sortProp, sortOrder, userId);
        if (CollUtil.isEmpty(videoList)) {
            return pageResult;
        }

        List<String> videoIdList = new ArrayList<>();
        for (Video video : videoList) {
            videoIdList.add(video.getId().toHexString());
        }

        List<VideoInfo> videoInfoList = queryVideoInfoByVideoId(sortProp, sortOrder, videoIdList);

        UserInfo userInfo = userService.queryUserInfoById(userId);

        List<VideoVo> videoVoList = new ArrayList<>();
        for (VideoInfo videoInfo : videoInfoList) {
            for (Video video : videoList) {
                if (videoInfo.getUserId().equals(video.getUserId())) {
                    VideoVo videoVo = new VideoVo();
                    videoVo.setId(videoInfo.getId().intValue());
                    videoVo.setNickname(userInfo.getNickName());
                    videoVo.setUserId(userId.intValue());
                    videoVo.setCreateDate(videoInfo.getCreateDate().intValue());
                    videoVo.setVideoUrl(video.getVideoUrl());
                    videoVo.setPicUrl(video.getPicUrl());
                    videoVo.setReportCount(videoInfo.getReportCount().intValue());
                    videoVo.setLikeCount(videoInfo.getLikeCount().intValue());
                    videoVo.setCommentCount(videoInfo.getCommentCount().intValue());
                    videoVo.setForwardingCount(videoInfo.getForwardingCount().intValue());
                    videoVoList.add(videoVo);
                }
            }
        }
        List<VideoVo> targetVideoVoList = ListPageUtil.listPaging(videoVoList, page, pagesize);
        pageResult.setItems(targetVideoVoList);
        pageResult.setCounts(videoList.size());
        pageResult.setPages((videoList.size() / pagesize) + 1);
        return pageResult;
    }

    /**
     * MongoDB查询video表
     *
     * @param sortProp
     * @param sortOrder
     * @param userId
     * @return
     */
    private List<Video> queryVideoByUserId(String sortProp, String sortOrder, Long userId) {
        // 根据userId去MongoDB的video表查询视频地址和封面图片
        Query query = Query.query(Criteria.where("userId").is(userId));
        if (sortOrder.equals("ascending")) {
            query.with(Sort.by(Sort.Order.asc(sortProp)));
        }
        if (sortOrder.equals("descending")) {
            query.with(Sort.by(Sort.Order.desc(sortProp)));
        }
        return mongoTemplate.find(query, Video.class);
    }

    /**
     * MySQL查询视频信息
     *
     * @param sortProp
     * @param sortOrder
     * @param userId
     * @return
     */
    private List<VideoInfo> queryVideoInfoByVideoId(String sortProp, String sortOrder, List<String> videoIdList) {
        QueryWrapper<VideoInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper = queryWrapper.in("video_id", videoIdList);
        if (sortOrder.equals("ascending")) {
            queryWrapper = queryWrapper.orderByAsc(sortProp);
        }
        if (sortOrder.equals("descending")) {
            queryWrapper = queryWrapper.orderByDesc(sortProp);
        }
        return videoInfoMapper.selectList(queryWrapper);
    }
}